#!/bin/bash

export 'PS4=[`date +"%Y-%m-%d %H:%M:%S"`]`basename $0`.$LINENO+ '
set -x
CPATH=$(cd `dirname $0`;pwd)
repo_url='http://dgggit09-rd.huawei.com/euleros/manifest.git'
HOME='/home/peulerosci'
PROJECT=''
MANIFEST=$1
PROJECT=$2

mkdir -p $HOME
git config --global credential.helper store
git config --global  user.name peulerosci

if [ ! -d ".repo" ]; then
    rm -rf init_repo_euleros
    curl -O http://dgggit09-rd.huawei.com/static/init_repo_euleros
    chmod 755 init_repo_euleros
    expect -c "
        set timeout -1
        spawn ./init_repo_euleros peulerosci
        expect {
            \"*(yes/no)*\" { send \"yes\r\"; exp_continue }
            \"*Username*\" { send \"peulerosci\r\"; exp_continue }
            \"*Password*\" { send \"EulerOS_123\r\"; exp_continue }
            eof { catch wait result; exit [lindex \$result 3] }
        }
        expect {
            eof { catch wait result; exit [lindex \$result 3] }
        }
"
    if [ $? -ne 0 ]; then
        echo "FAILED: init_repo_euleros peulerosci !!!"
        exit 1
    fi
    rm -f init_repo_euleros

    PATH=~/bin:$PATH
    expect -c "
        set timeout 600
        spawn repo init -u $repo_url -b master -m $MANIFEST --no-repo-verify --repo-branch=euleros --no-clone-bundle
        expect {
            \"*(yes/no)*\" { send \"yes\r\"; exp_continue }
            \"*Username*\" { send \"peulerosci\r\"; exp_continue }
            \"*Password*\" { send \"EulerOS_123\r\"; exp_continue }
            eof { catch wait result; exit [lindex \$result 3] }
        }
        expect {
            eof { catch wait result; exit [lindex \$result 3] }
        }
"
    rm -rf .repo
    repo init -u $repo_url -b master -m $MANIFEST --no-repo-verify --repo-branch=euleros --no-clone-bundle
    if [ $? -ne 0 ]; then
        echo "FAILED: repo init $MANIFEST !!!"
        exit 1
    fi
fi


PATH=~/bin:$PATH
which repo

n=`lscpu | grep '^CPU(s):' | awk '{print $2}'`
let n=$n/4
if ps -ef | grep repo_sync.sh &>/dev/null; then
    n=2
fi

if [ -d ./.repo ];then
cd ./.repo
find . -name '*.lock' |xargs rm -rf
cd -
fi
#full repo sync task
if [ "x$PROJECT" == "x" ];then 
		expect -c "
		set timeout -1
		spawn repo sync --force-sync $PROJECT -c -j$n --no-clone-bundle --no-tags --force-broken
		expect {
			\"*(yes/no)*\" { send \"yes\r\"; exp_continue }
			\"*Username*\" { send \"peulerosci\r\"; exp_continue }
			\"*Password*\" { send \"EulerOS_123\r\"; exp_continue }
			eof { catch wait result; exit [lindex \$result 3] }
		}
		expect {
			eof { catch wait result; exit [lindex \$result 3] }
		}
	"
	ret=$?
	if [ $ret -eq 0 ]; then
		echo "finished repo sync $MANIFEST $PROJECT success"
		exit 0
	else
		echo "FAILED: repo sync $MANIFEST $PROJECT"
		exit 1
	fi
else    #patchset task	
	grep -nr "\"$PROJECT\"" ./.repo/manifests/EulerOS_docker.xml &>/dev/null
	if [ $? -eq 0 ]; then
		for repodir in next EulerOS_next EulerOS_next_centos7.5
		do
			if [ -d ../$repodir ]; then
				cd ../$repodir
				expect -c "
					set timeout -1
					spawn repo sync --force-sync $PROJECT -c -j$n --no-clone-bundle --no-tags
					expect {
						\"*(yes/no)*\" { send \"yes\r\"; exp_continue }
						\"*Username*\" { send \"peulerosci\r\"; exp_continue }
						\"*Password*\" { send \"EulerOS_123\r\"; exp_continue }
						eof { catch wait result; exit [lindex \$result 3] }
					}
					expect {
						eof { catch wait result; exit [lindex \$result 3] }
					}
				"	
				repo sync --force-sync $PROJECT -c -j$n --no-clone-bundle --no-tags	
				ret=$?
				if [ $ret -ne 0 ]; then
					echo "FAILED: repo sync $MANIFEST $PROJECT"
					exit 1
				fi
			fi
		done	
	else
		expect -c "
			set timeout -1
			spawn repo sync --force-sync $PROJECT -c -j$n --no-clone-bundle --no-tags
			expect {
				\"*(yes/no)*\" { send \"yes\r\"; exp_continue }
				\"*Username*\" { send \"peulerosci\r\"; exp_continue }
				\"*Password*\" { send \"EulerOS_123\r\"; exp_continue }
				eof { catch wait result; exit [lindex \$result 3] }
			}
			expect {
				eof { catch wait result; exit [lindex \$result 3] }
			}
		" 
		repo sync --force-sync $PROJECT -c -j$n --no-clone-bundle --no-tags
		ret=$?
		if [ $ret -eq 0 ]; then
			echo "finished repo sync $MANIFEST $PROJECT success"
			exit 0
		else
			echo "FAILED: repo sync $MANIFEST $PROJECT"
			exit 1
		fi	
	fi
fi
